<template>
  <div>
    <el-form inline>
      <el-form-item label="视频名称">
        <el-input v-model="name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-button style="margin-left: 10px" type="primary" @click="search"
        >查 询</el-button
      >
      <el-button style="margin-left: 10px" type="primary" @click="uploadHandler"
        >上 传</el-button
      >
    </el-form>

    <el-dialog :visible.sync="show" title="上传"
      ><el-upload
        multiple
        style="margin-left: 10px"
        :action="urlPpt"
        :on-success="handleFileSuccess"
        :on-error="handlerFileError"
      >
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
    </el-dialog>
    <el-dialog width="1200px" :visible.sync="showIframe" title="预览">
      <iframe height="700px" width="100%" :src="iframeSrc"></iframe>
    </el-dialog>
    <el-table :data="tableData">
      <el-table-column label="名称" prop="name" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.url"
            @click="detailHandler(scope.row)"
            size="mini"
            type="text"
            >查看</el-button
          >
          <el-button @click="deleteHandler(scope.row)" size="mini" type="text"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
import host from "@/utils/host";
import video from "@/api/video/index";

//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json 文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import 引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      tableData: [],
      show: false,
      urlPpt: "",
      iframeSrc: "",
      showIframe: false,
      name: "",
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  //计算属性 类似于 data 概念
  computed: {},
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    search() {
      this.getTotal();
      this.list(1);
      this.currentPage = 1;
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.list(currentPage);
    },
    deleteHandler: async function (data) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          video.deleteByURL(data).then((res1) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.list(1);
            this.getTotal();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    uploadHandler: async function () {
      const port = 9080;
      this.urlPpt = `http://${host}:${port}/elearning/video/uploadVideo`;
      this.show = true;
    },
    detailHandler: async function (data) {
      console.log(data.url);
      window.open(data.url);
    },
    handleFileSuccess() {
      this.$message({
        message: "上传成功",
        type: "success",
      });
      this.name = "";
      this.getTotal();
      this.list(1);
    },
    handlerFileError(data) {
      this.$message({
        message: `${data.message}`,
        type: "error",
      });
    },
    list: async function (currentPage) {
      const res = await video.list(this.name, currentPage, this.pageSize);
      this.tableData = res.data;
    },
    getTotal: async function () {
      const res = await video.getTotal(this.name);
      this.total = res.data;
    },
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {
    this.list(1);
    this.getTotal();
  },
};
</script>
<style scoped></style>
